package com.boss.fileapp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boss.api.dto.ApiResultDTO;
import com.boss.fileapp.schedule.SalaryAutoReportTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <br>Title:
 * <br>Description:
 * <br>Author:
 * <br>Date:
 */
@RestController
public class CronConfigController {

    private static Logger logger = LoggerFactory.getLogger(CronConfigController.class);

    /** 错误码 **/
    private static final String ERROR_CODE = "999";

    @Autowired
    private SalaryAutoReportTask salaryAutoReportTask;

    /**
     * 修改定时任务
     * @param params
     * @return
     */
    @PostMapping("/editCron")
    public ApiResultDTO editCron(@RequestBody String params) {
        logger.info(" editCron into params: " + params);
        if (StringUtils.isEmpty(params)) {
            return ApiResultDTO.error(ERROR_CODE, "参数为空！");
        }
        JSONObject jsonObject = JSON.parseObject(params);
        if (!jsonObject.containsKey("mofDivCode")) {
            return ApiResultDTO.error(ERROR_CODE, "参数【mofDivCode】为空！");
        }
        if (!jsonObject.containsKey("day")) {
            return ApiResultDTO.error(ERROR_CODE, "参数【day】为空！");
        }
        String mofDivCode = jsonObject.getString("mofDivCode");
        String day = jsonObject.getString("day");
        // 修改定时任务
        salaryAutoReportTask.editCronTask(mofDivCode, day);
        return ApiResultDTO.success();
    }

}
